/**
 * @Description: test
 * @Author: Hu Outstanding
 * @Date: 2022/2/22 12:37
 */
package com.aaa.three.aop;

import com.aaa.three.mapper.LogMapper;
import com.aaa.three.pojo.domain.AdminInfo;
import com.aaa.three.pojo.domain.LogData;
import com.aaa.three.result.AjaxResult;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

public class LogAop {
    @Autowired
    private LogMapper mapper;


    public void after(JoinPoint joinPoint, Log aaaLog, AjaxResult result){

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpServletResponse response = requestAttributes.getResponse();

        AdminInfo admin = (AdminInfo) request.getSession().getAttribute("admin");
        String username = admin.getAname();
        String    operateDept = admin.getDept().getDname();
        String    operateOffice = admin.getOffice().getOname();
        String    aphone = admin.getAphone();

        String    operateType = aaaLog.value();
        Integer   operateState = result.getCode();
        String    operateIP = request.getRemoteAddr();
        Date      operateDate = new Date();
        Object[]  data = joinPoint.getArgs();


        LogData logData = new LogData(null, username, operateDate, operateIP, operateType,
                operateDept, operateState, Arrays.toString(data),operateOffice,aphone);
        mapper.addLog(logData);
    }



}
